package jinyilw.swing.visuals.Jwindow;

import jinyilw.swing.ColorTools;
import jinyilw.swing.DragCompUtils;
import jinyilw.swing.visuals.TransRootPane;

import javax.swing.JRootPane;
import javax.swing.JWindow;
import java.awt.Rectangle;
import java.awt.Window;
import java.awt.geom.RoundRectangle2D;

public class TransWindow extends JWindow
{
	private static final long serialVersionUID = -1095860454408238147L;
	private TransRootPane rootPane;
	protected boolean rounded = true;
	protected int arc = 8;

	public TransWindow()
	{
		initGUI();
	}

	public TransWindow(Window owner)
	{
		super(owner);
		initGUI();
	}

	private void initGUI()
	{
		JRootPane rp = getRootPane();
		if (rp instanceof TransRootPane)
			rootPane = (TransRootPane) rp;
		setBackground(ColorTools.transparentColor);
	}

	public void setBGOpacity(int opacity)
	{
		if (rootPane != null)
			rootPane.setColorOpacity(opacity);
	}

	public void setWindowAlpha(float alpha)
	{
		if (rootPane != null)
			rootPane.setAlpha(alpha);
	}

	public Rectangle calculateWindowRect()
	{
		return null;
	}

	public boolean isRounded()
	{
		return rounded;
	}

	public void setRounded(boolean b)
	{
		rounded = b;
		if (b)
			setShape(new RoundRectangle2D.Double(0, 0, getWidth(), getHeight(),
					arc, arc));
		else
			setShape(null);
	}

	public void setResizable(boolean b)
	{
		if (b)
			DragCompUtils.setResizeable2(getContentPane(), this);
	}

	public void setMovable(boolean b)
	{
		if (b)
			DragCompUtils.setMovable(getContentPane(), this);
	}

	@Override
	public void setBounds(int x, int y, int width, int height)
	{
		super.setBounds(x, y, width, height);
		if (width == 0 || height == 0)
			return;
		if (rounded)
			setShape(
					new RoundRectangle2D.Double(0, 0, width, height, arc, arc));
	}

	@Override
	protected JRootPane createRootPane()
	{
		return new TransRootPane(this);
	}
}
